<template>
  <div class="goods-image">
    <div class="large" v-show="!isOutside">
      <img :src="images[activeIndex]" :style="{left:largePos.left,top:largePos.top}" alt="" />
    </div>
    <div class="middle" ref="target">
      <img :src="images[activeIndex]" alt="" />
      <div class="layer" :style="{left:layerPos.left,top:layerPos.top}" v-show="!isOutside"></div>
    </div>
    <ul class="small">
      <li v-for="(item, index) in images" :key="index">
        <img @mouseenter="activeIndex = index" :src="item" alt="" />
      </li>
    </ul>
  </div>
</template>

<script setup name="GoodsImage">
import { ref, defineProps, watch } from "vue";
import { useMouseInElement } from '@vueuse/core'
const props = defineProps({
  images: {
    type: Array,
    default: () => []
  }
});
const target = ref(null)
const activeIndex = ref(0);
const { elementX, elementY, isOutside } = useMouseInElement(target)
// 监听鼠标的两个坐标值

// 定义响应式数据，更新数据的时候更新视图
const layerPos = ref({ left: 0, top: 0 })
const largePos = ref({ left: 0, top: 0 })


watch([elementX, elementY], () => {
  // -100  =>  让盒子和鼠标的中心对齐
  let left = elementX.value - 100
  let top = elementY.value - 100

  if (left < 0) left = 0
  if (left > 200) left = 200
  if (top < 0) top = 0
  if (top > 200) top = 200

  layerPos.value.left = left + 'px'
  layerPos.value.top = top + 'px'

  largePos.value.left = -left * 2 + 'px'
  largePos.value.top = -top * 2 + 'px'
})
</script>

<style lang="less" scoped>
.goods-image {
  width: 480px;
  height: 400px;
  position: relative;
  display: flex;
  .large {
    position: absolute;
    top: 0;
    left: 412px;
    width: 400px;
    height: 400px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    background-repeat: no-repeat;
    background-size: 800px 800px;
    background-color: #f8f8f8;
    overflow: hidden;
    z-index: 401;
    img {
      max-width: 800px;
      max-height: 800px;
      width: 800px;
      height: 800px;
      position: absolute;
      left: 0;
      top: 0;
    }
  }
  .middle {
    width: 400px;
    height: 400px;
    background: #f5f5f5;
    position: relative;
    cursor: move;
    .layer {
      width: 200px;
      height: 200px;
      background: rgba(0, 0, 0, 0.2);
      left: 0;
      top: 0;
      position: absolute;
    }
  }
  .small {
    width: 80px;
    li {
      width: 68px;
      height: 68px;
      margin-left: 12px;
      margin-bottom: 15px;
      cursor: pointer;
      &:hover,
      &.active {
        border: 2px solid @xtxColor;
      }
    }
  }
}
</style>
